$(document).ready(function() {
    var slidedRev = 1;
    
    jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
        return this;
    }
    
    $("#addReview a").click(function(){
        var selected = $(this).attr('href');	
        $.scrollTo(selected, 500);		
        return true;
    });
    
    $('.showUndefided').click(function() {
        var id = $(this).attr('id').replace(/\D+/g,'');
        slidedRev ? $('#'+id+'revewCont').slideDown(300) : $('#'+id+'revewCont').slideUp(300);
        slidedRev = slidedRev ? 0 : 1;
    });
    
    $('#hideRules').click(function() {
        if($('#rulesField').css('display') == 'none') $('#rulesField').slideDown(100);
        else $('#rulesField').slideUp(100);
    });
    
    $('#form').submit(function() {
        $('#textError').css({'display':'none'});
        $('#ratingError').css({'display':'none'});
        var rating = $('#form').serializeArray();
        console.log(rating);
        var text = tinyMCE.get('editor').getContent();
        if(!text) {
            $(tinymce.activeEditor.getBody()).css("background-color",'#FBC2C4');
            $(tinymce.activeEditor.getBody().parentNode).css("background-color",'#FBC2C4');
            $('#textError').css({'display':'block'});
        } else {
            $(tinymce.activeEditor.getBody()).css("background-color",'#FFFFFF');
            $(tinymce.activeEditor.getBody().parentNode).css("background-color",'#FFFFFF');
        }
        
        var isSelectRating = 0;
        for(var i = 0; i < rating.length; i++) 
            if(rating[i].name.indexOf('Reviews[ratings]') + 1) isSelectRating = 1;
        if(!isSelectRating) $('#ratingError').css({'display':'block'});
        if(!isSelectRating || !text) return false;
        return true;
    });
    
    $('.showMoreSort').click(function() {
        if($('.moreSort').css('display') == 'none')
            {
            $('.short_for_text').hide();
            $('.moreSort').show();
            }
        else 
            {
            $('.moreSort').hide();
            $('.short_for_text').show();
            }
    });
});